<?php
function args_validate($args,$url,$argument='post')
{

    if('post'==$argument)$argument=&$_POST;
    elseif('get'==$argument)$argument=&$_GET;
    else exit('unknow argument:'.$argument);

    //已有参数类型
    $types=array(
        'realname'  =>'真实姓名',
        'qq'        =>'QQ号码',
        'account'   =>'游戏帐号',
        'email'     =>'电子邮件',
        'cellphone' =>'手机号码',
        'phone'     =>'电话号码',
        'fixed_phone'=>'固定电话号码',
        'idcard'    =>'身份证号',
        'date'      =>'日期',
        'time'      =>'时间',
        'datetime'  =>'日期时间',
        'url'       =>'url地址',

        'gender'    =>array(
            'type'  =>'list',
            'list'  =>array('男','女'),
            'tip'   =>'性别'
        ),
        'age'       =>array(
            'type'  =>'number',
            'little'=>1,
            'big'   =>200,
            'tip'   =>'年龄'
        ),
        'account_grade'=>array(
            'type'  =>'number',
            'little'=>1,
            'big'   =>100,
            'tip'   =>'账号级别'
        ),
        'zone'      =>array(
            'type'  =>'list',
            'list'  =>array('电信一区','网通一区'),
            'tip'   =>'游戏大区'
        )
    );
    $data=array();
    if(!is_array($args))exit('types should be an array.');
    foreach($args as $name=>$type)
    {
        if(is_string($type))
        {
            if(is_int($name))$name=$type;

            if(isset($types[$type]))
            {
                $tmp=$types[$type];
                if(is_string($tmp))
                {
                    $t=$type;
                    $tip=$tmp;
                }
                elseif(is_array($tmp))
                {
                    $type=$tmp;
                    $t=$type['type'];
                    if(isset($type['tip']))$tip=$type['tip'];
                    else $tip=$name;
                }
            }
            else exit('unknow type:'.$type.' for arg:'.$name);
        }
        elseif(is_array($type))
        {
            $t=$type['type'];
            if(is_int($name))$name=$t;
            if(isset($type['tip']))$tip=$type['tip'];
            else $tip=$name;
        }
        else exit("wrong type for types:$name");

        if(!isset($argument[$name]))
        {
            if(isset($type['optional']) && $type['optional'])continue;
            else
            {
                exit('lack of argument:'.$name);
            }
        }
        $value=trim($argument[$name]);

        if(isset($type['blank']) && $type['blank'])
        {
            if(''==$value)
            {
                $data[$name]=$value;
                continue;
            }
        }

        $tip='无效的'.$tip;
        
        switch($t)
        {
        case 'none':
            break;
        case 'no_blank':
            if(0==strlen($value))js_go($url,$tip);
            break;
        case 'realname':
            if(!is_realname($value))js_go($url,$tip);
            break;
        case 'qq':
            if(!is_qq($value))js_go($url,$tip);
            break;
        case 'account':
            if(!is_51account($value))js_go($url,$tip);
            break;
        case 'email':
            if(!is_email($value))js_go($url,$tip);
            break;
        case 'cellphone':
            if(!is_cellphone($value))js_go($url,$tip);
            break;
        case 'fixed_phone':
            if(!is_fixed_phone($value))js_go($url,$tip);
            break;
        case 'phone':
            if(!is_phone($value))js_go($url,$tip);
            break;
        case 'idcard':
            if(!is_idcard($value))js_go($url,$tip);
            break;
        case 'date':
            break;
        case 'time':
            break;
        case 'datetime':
            break;
        case 'url':
            if(!is_url($value))js_go($url,$tip);
            break;

        case 'digit':
            if(!is_digit($value,$type['little'],$type['big']))js_go($url,$tip);
            break;
        case 'number':
            if(!is_number($value,$type['little'],$type['big']))js_go($url,$tip);
            break;
        case 'list':
            if(isset($type['list']))
            {
                $list=$type['list'];
                if(is_array($list))
                {
                    if(!in_array($value,$list))js_go($url,$tip);
                }
                else exit('list for arg:'.$name.'should be an array');
            }
            else exit('lack list for arg:'.$name);
            break;
        default:
            exit('unknow type:'.$t.' for arg:'.$name);
        }
        $data[$name]=$value;
    }
    return $data;
}
function is_url($url)
{
    return preg_match('#^(ftp|http|https)://\P{C}+\.\P{C}+$#iu',$url);
}
function is_date()
{
}
function is_time()
{
}
function is_datetime()
{
}
function is_digit($value,$little,$big)
{
    return preg_match('/^\d{'.$little.','.$big.'}$/',$value);
}
function is_number($value,$little,$big)
{
    return preg_match('/^\d+$/',$value) && $value>=$little && $value<=$big;
}
function is_realname($value)
{
    return preg_match("/^[\x{4e00}-\x{9fa5}]{2,20}$/u",$value);
}
function is_qq($value)
{
    return preg_match('/^[1-9][0-9]{4,}$/',$value);
}
function is_51account($value)
{
    return preg_match('/^[a-z][a-z0-9_]{1,14}[a-z0-9]$/i',$value);
}
function is_email($value)
{
    return preg_match('/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/',$value);
}

function is_cellphone($value)
{
    return preg_match('/^1[358]\d{9}$/',$value);
}

function is_phone($value) {  
    return preg_match('/^1(3|5|8)\d{9}$|^(([0\+]\d{2,3}-)?(0\d{2,3})-)?(\d{7,8})(-(\d{3,}))?$/', $value);
}
function is_fixed_phone($value){
    return preg_match('/^(([0\+]\d{2,3}-)?(0\d{2,3})-)?(\d{7,8})(-(\d{3,}))?$/', $value);
}

function is_idcard($value)
{
    return preg_match('/^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}[0-9Xx]$|^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$/',$value);
}

function js_go($url,$msg='')
{
    $s='<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <script language="javascript">
';
    if($msg)$s.="alert('$msg');
";
    $s.='window.location="'.$url.'";
        </script>';
    echo $s;
    exit();
}
